/** @file    FormNetworkManager.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormNetworkManager.cpp 2867 2010-08-07 20:44:51Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormNetworkManager class.
 * The header for this class can be found in FormNetworkManager.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormNetworkManager.h"
 #include "ui_FormNetworkManager.h"
 #include <core/NetworkManager.h>
 #include <interfaces/services/DiscoveryUdp.h>

 #include <QtGui/QInputDialog>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::FormNetworkManager, "wosh::gui::IWidgetGeneric", 1.0, _static_FormNetworkManager )
 WOSH_GUI_MAP(wosh::gui::FormNetworkManager, "wosh::NetworkManager", 0.0, _static_Map_FormNetworkManager )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormNetworkManager::FormNetworkManager(QWidget *parent)
	: IWidgetGeneric(parent) {
	this->m_ui = new Ui::FormNetworkManager();
	this->m_ui->setupUi(this);

	IWidgetGeneric::setTargetURI(_NetworkManager_URI);

	// tab Information [0]
	this->m_ui->tableViewNeighbours->setMethodList(_NetworkManager_METHOD_Neighbours_list);
	connect(this->m_ui->tableViewNeighbours, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

	connect(this->m_ui->wUpBarNetworkTree, SIGNAL(updateClicked()), this, SLOT(refreshNetworkTree()));

	// tab Management [1]

	// tab Hosts [2]
	this->m_ui->tableViewHosts->setMethodList(_NetworkManager_METHOD_Hosts_list);
	connect(this->m_ui->tableViewHosts, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

	// tab Channels [3]
	this->m_ui->tableViewChannels->setMethodList(_NetworkManager_METHOD_Channels_list);
	connect(this->m_ui->tableViewChannels, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

	// tab Protocols [4]
	this->m_ui->tableViewProtocols->setMethodList(_NetworkManager_METHOD_Protocols_list);
	connect(this->m_ui->tableViewProtocols, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

	// tab Routing [5]
	this->m_ui->tableViewRoutesStatic->setMethodList(_NetworkManager_METHOD_RoutesStatic_list);
	connect(this->m_ui->tableViewRoutesStatic, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	this->m_ui->tableViewRoutesDynamic->setMethodList(_NetworkManager_METHOD_RoutesDynamic_list);
	connect(this->m_ui->tableViewRoutesDynamic, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

 }

FormNetworkManager::~FormNetworkManager() {
	delete this->m_ui;
 }

void FormNetworkManager::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormNetworkManager::refreshAll() {
	this->setWindowTitle( QString("Network Manager [%1]").arg(this->getTargetURI().getKernelName().c_str()) );
	this->m_ui->tableViewHosts->refreshData();
	this->m_ui->tableViewChannels->refreshData();
	this->m_ui->tableViewProtocols->refreshData();
	this->m_ui->tableViewNeighbours->refreshData();
	refreshNetworkTree();
	this->m_ui->tableViewRoutesStatic->refreshData();
	this->m_ui->tableViewRoutesDynamic->refreshData();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormNetworkManager::refreshNetworkTree() {
	executeRemoteMethod( _NetworkManager_METHOD_NetworkTree_list );
	this->m_ui->wUpBarNetworkTree->sayRetrieving();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormNetworkManager::btnForceDiscovery_clicked() {
	Message* message = new Message( new Request( _Discovery_METHOD_discover ) );
	message->getDestination().fromString( ":Bundles/Discovery" );
	postMessageForTarget(message);
 }

void FormNetworkManager::btnChannelCreate_clicked() {
	bool ok;
	QString text = QInputDialog::getText(this, this->windowTitle(), tr("Connection String: (PROTOCOL://IP_ADDRESS:PORT)\n(such as TCP://192.168.0.1:9797, usually the IP refers to localhost)"), QLineEdit::Normal, "", &ok);
	if ( !ok || text.isEmpty())
		return;
	string proto = text.toLatin1().data();
	executeRemoteMethod( _NetworkManager_METHOD_CreateChannel, proto );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormNetworkManager::busMessage( const Message& message, const Bus* ) {
	if ( message.isEmpty() || !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) ) return;
	const wosh::Response* response = message.getContent()->asResponse();

	this->m_ui->tableViewHosts->evalResponse(response);
	this->m_ui->tableViewChannels->evalResponse(response);
	this->m_ui->tableViewProtocols->evalResponse(response);
	this->m_ui->tableViewNeighbours->evalResponse(response);
	this->m_ui->tableViewRoutesStatic->evalResponse(response);
	this->m_ui->tableViewRoutesDynamic->evalResponse(response);

	if ( response->getMethod() == _NetworkManager_METHOD_CreateChannel ) {
		if ( response->hasData() )
			this->m_ui->tableViewChannels->refreshData();
	 }
	promptMethodResponse(message);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
